package cn.itsource.base.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class EmailUtil {
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;


    public void sendMessage(String title,String content,String toQQ) {
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            //true - 能够添加附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom(from);
            helper.setSubject(title);
            //true - 能够再内容中编写html标签 - 会解析
            helper.setText(content,true);
            //添加附件
//        helper.addAttachment("p7.jpg",new File("C:\\Users\\admin\\Pictures\\BL\\bl22.jpg"));
//        helper.addAttachment("g1.zip", new File("C:\\Users\\admin\\Pictures\\BL.rar"));
            //收件人
            helper.setTo(toQQ);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            System.out.println("发送邮件失败！！！");
            throw new RuntimeException(e);
        }
    }
}
